#--
#   Television Episode Downloader
#   - Initial Release: 4-02-08
#       - Find URL from epguides.com
#       - filenames to rename_list.csv
#   - Internal Dev Release 1:
#       - construct dictionary entry for filename:[series, season, ep]
#       - correctly interact with rename_list, make batch/sh/etc and better
#
#   Rob Dennis - Lurchington Q. Delacroix ESQ. and man about town
#
#   This script is inspired and is thanks to:
#   -   Rob Meerman's tvrenamer perl script
#           http://www.robmeerman.co.uk/coding/file_renamer
#   -   Softpointer's Tag&Rename
#           http://www.softpointer.com/tr
#   
#--

import os,re,glob,csv,time,sys,string

def checkThenAppendName(fileList):

    if not os.path.exists("rename_list.csv"):
        os.system("touch rename_list.csv")

    inF = open("rename_list.csv","rb")
    textDump=inF.read()
##    print "textDump",textDump
    inF.close()

    inF = open("rename_list.csv","rb")
    textList=inF.readlines()
##    print "original textList",textList
    inF.close()


    for filename in fileList:
        if filename in textDump:
##            print filename,"exists"
            pass
        else:
##            print filename,"does not exist"
            textList.append(filename+",\n")

##    print "pre sort",textList
    textList.sort()

##    print "post sort",textList
    if len(textList)>0:
        outF=open("rename_list.csv","wb")
    
        outF.writelines(textList)
    
        outF.close()

def checkThenRename():

    inL = open("rename_list.csv","rb")

    inList = csv.reader(inL)

    newRows = []

    for row in inList:
        if len(row[1])==0:
            row[1]=reshapeName(row[0])
##        print row
        newRows.append(row)

    inL.close()

##    print newRows
    
    outL = open("rename_list.csv","wb")
    outList = csv.writer(outL)
    outList.writerows(newRows)
    outL.close()

def reshapeName(oldName):
    return "sup "+oldName

def searchInfo(fileList):
    print "searching for season in:\n"

    seasonRE1=re.compile(r"(?<=[\ _xs\.])[0-9]+",re.IGNORECASE)

    for fileN in fileList:
        print ">>>"
        print fileN
        seasonNum1=seasonRE1.search(fileN).group(0)
        if len(seasonNum1)>0:
            print int(seasonNum1)
        else:
            print "no season parsed"            
        print "<<<"

    print "searching for episode in:\n"

    episodeRE1=re.compile(r"(?<=[\ _xe\.])[0-9]+",re.IGNORECASE)
    
    for fileN in fileList:
        print ">>>"
        print fileN
        episodeNum1=episodeRE1.search(fileN).group(0)
        if len(episodeNum1)>0:
            print int(episodeNum1)
        else:
            print "no season parsed"            
        print "<<<"


    seriesRE1=re.compile(r".*(?=(Season))",re.IGNORECASE)

    series = seriesRE1.search(os.path.split(sys.path[0])[1]).group(0)

    norm = string.maketrans("","")

    series=series.translate(norm,string.whitespace)

    print r"http://epguides.com/"+series
        
    

    

def main():
    startTime=time.time()
    
    newFileNameList = []
    fileTypes = re.compile(r'.*(avi|mkv)$',re.IGNORECASE)
    fileNameList=glob.glob("*.*")
    for fileN in fileNameList:
##        print fileN
        if fileTypes.match(fileN.strip()):
##            print "matched"
            newFileNameList.append(fileN)
##    print newFileNameList

    checkThenAppendName(newFileNameList)
    searchInfo(newFileNameList)

##    checkThenRename()

    

    print time.time()-startTime

main()
